package com.uroad.lib.util.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * 用于Activity跳转的工具类
 */
public class ActivityUtil {

	public static void openActivity(Activity activity, Class<?> pClass) {
		openActivity(activity, pClass, null);
	}

	/**
	 * 假如Activity存在，则打开这个Activity而不是新建,同时清理该Activity上面的所有Activity
	 * **/
	public static void openActivityFromHistory(Activity activity,
			Class<?> pClass) {
		Intent intent = new Intent(activity, pClass);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//生效的首要条件就是要目标activity存在task中，但是因为下载耗时过长，可能在途中目标activity已经被安卓自己干掉了（猜测）。
//		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);//这个方法不可用，会导致整个栈只剩下最新建的activity
		activity.startActivity(intent);
	}

	/**
	 * 清理activity栈，并打开指定界面
	 * **/
	public static void openActiviyAfterClearStack(Activity activity,
												  Class<?> pClass){
		Intent intent = new Intent(activity, pClass);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);//这个方法会导致整个栈只剩下最新建的activity
		activity.startActivity(intent);
	}

	public static void openActivity(Activity activity, Class<?> pClass,
			Bundle pBundle) {
		Intent intent = new Intent(activity, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		activity.startActivity(intent);
	}


	public static void openActivity(Activity activity, String pAction) {
		openActivity(activity, pAction, null);
	}

	public static void openActivity(Activity activity, String pAction,
			Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		activity.startActivity(intent);
	}

	public static void openActivityForResult(Activity activity,
											 Class<?> pClass, Bundle pBundle, int requestCode) {
		Intent intent = new Intent(activity, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		activity.startActivityForResult(intent, requestCode);
		// pendingTransition_start(activity);
	}

	public static void openAnimActivity(Activity activity,Class<?> pClass,int inAnim,int outAnim,Bundle pBundle){
		Intent intent=new Intent(activity,pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		activity.startActivity(intent);
		activity.overridePendingTransition(inAnim,outAnim);
	}
}
